import 'package:get/get.dart';
import 'package:flutter/material.dart';
import '../../home/views/home_view.dart';
import '../../category/views/category_view.dart';
import '../../service/views/service_view.dart';
import '../../user/views/user_view.dart';
import '../../cart/views/cart_view.dart';

// 对应Controller就是对应页面的业务逻辑抽离
class TabsController extends GetxController {
  //TODO: Implement TabsController
  //  定义PageView的控制器
  PageController pageController = Get.arguments == null
      ? PageController(initialPage: 0)
      : PageController(
          initialPage: Get.arguments['index'], // tab页面默认加载的索引pageView
        ); 

  // 定义所有tab页面
  List<Widget> pages = [
    const HomeView(),
    const CategoryView(),
    CartView(),
    const ServiceView(),
    const UserView(),
  ];

  // 定义当前tab页面索引
  RxInt currentIndex = 0.obs; // 底部tab切换索引

  void setCurrentIndex(int index) {
    currentIndex.value = index;
    update(); // 更新UI
  }

  // tabs默认默认显示页面
  void showInitialPage() {
    if (Get.arguments != null) {
      currentIndex.value = Get.arguments['index'];
      update();
    }
  }

  @override
  void onInit() {
    super.onInit();
    showInitialPage();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }
}
